/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package african11;

import java.util.Arrays;
import java.util.HashMap;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashSet;
import static chanlib.Chan_IO.*;

/**
 * @author robit
 */
public class VanishingNumber{
    /*
      All neccesary to be defined
      including:
      variable, inner class
    */
    // static int A;
    // static void B();
    // static class C

    /*
       One calculation fits all of the cases.
    */
    static void preReckon(){
        return;
    }
    static HashSet<BigDecimal> patterns;
    static int rank(BigDecimal x){
        if(patterns.contains(x))return Integer.MAX_VALUE;
        patterns.add(x);
        BigDecimal tripleX=new BigDecimal(3).multiply(x);
        int integral=tripleX.intValue();
        if(integral==1)return 0;
        else if(integral==0)return 1+rank(tripleX);
        else return 1+rank(tripleX.subtract(new BigDecimal(2)));
    }
    /*
      calculate the answer
    */
    private static void reckon(int line) {
        int N=Int();
        BigDecimal[]d =new BigDecimal[N];
        String s[]=new String[N];
        for(int i=0;i<N;i++){
            d[i]=new BigDecimal(String());
        }
        
        class Pair implements Comparable<Pair>{
            int rank;
            BigDecimal value;

            public Pair(int rank, BigDecimal value) {
                this.rank = rank;
                this.value = value;
            }

            public int compareTo(Pair o) {
                if(this.rank<o.rank)return -1;
                else if(this.rank==o.rank)return this.value.compareTo(o.value);
                else return 1;
            }
        }
        patterns =new HashSet<BigDecimal>();
        Pair numbers[] = new Pair[N];
        for(int i=0;i<N;i++){
            patterns.clear();
            BigDecimal value=d[i];
            int rank=rank(d[i]);
            if(rank<0)rank=Integer.MAX_VALUE;
            numbers[i]=new Pair(rank, value);
        }
        
        Arrays.sort(numbers);

        String output=String.format("Case #%d:    ",line);
        writeln(output, OUTFILE);
        for (Pair e : numbers) {
            writeln(e.value.toPlainString(),OUTFILE);
        }


    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        /*
           Pre-computing for all cases
        */

        // Just in case!
        //preReckon();
        //while((char)read()!='P');

        /*
          Input-output
        */
        boolean test=false;
        boolean small=false;
        String prob="A";
        String filename;
        if(test) filename="test";
        else{
            if(small) filename=prob+"-small-practice";
            else filename=prob+"-large-practice";
        }
        try {
            /*
               Init IO,plus determination of input line number
             */
            int lineNums = initFileIO(filename);
            for (int i = 1; i <= lineNums; i++) {
                reckon(i);
            }
        } finally {
            endIO();
        }
    }

}
